package dao;

import java.sql.*;
import static dao.DbProp.getDriver;

/**
 * Created by Administrator on 2018/12/13.
 */
public class MyDb {

    private Connection getConn() {
        Connection conn = null;
        try {
            Class.forName(getDriver());
            conn = DriverManager.getConnection(DbProp.getUrl(), DbProp.getUser(), DbProp.getPass());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return conn;
    }

    public boolean executeUpdate(String sql){
        try{
            Connection conn = getConn();
            Statement st = conn.createStatement();
            st.executeUpdate(sql);
            return true;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return  false;
    }

    public boolean isExist(String sql,String[] args){
        try {
            Connection conn=getConn();
            PreparedStatement pst = conn.prepareStatement(sql);
            for (int i = 0; i <args.length ; i++) {
                pst.setString(i+1,args[i]);
            }
            ResultSet rs = pst.executeQuery();
            return rs.next();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return false;
    }


}
